import { App } from './app';
import Util from './util';
import { Page } from './page';

App.registerCrud({
	route: 'authority',//路由名称
	url: $.config('apiUrl') + 'auth',
	title: '权限管理',//分页标题
	capable: {
		create: true
	},
	columns: [{
		dataField: 'id',
		caption: '编号'
	}, {
		dataField: 'jobId',
		caption: '工种'//名称
	}, {
		dataField: 'moduleId',
		caption: '功能模块'
	}, {
		minWidth: 120,
		caption: '操作',
		buttons: [{
			title: '编辑',
			url: '#/authority/edit/{{id}}'//#/路由名称/edit/{{id}}
		}]
	}],
	form: {
		readOnly: false,
		showColonAfterLabel: true,
		showValidationSummary: false,
		colCount: 1,
		beforeSubmit: function(form, id) {
			form.option('formData', Object.assign({type: 'admin'}, form.option('formData')));

			return true;
		},
		items: [{
			dataField: 'id',
			validationRules: [{ type: "required" }],
			label: { text: '编号' }
		}, {
			dataField: 'jobId',
			validationRules: [{ type: "required" }],
			label: { text: '工种' }
		}, {
			dataField: 'moduleId',
			validationRules: [{ type: "required" }],
			label: { text: '功能模块' }
		}]
	}
});

App.registerCrud({
	route: 'user',//路由名称
	url: $.config('apiUrl') + 'user',
	title: '用户管理',//分页标题
	capable: {
		create: true
	},
	columns: [{
		dataField: 'id',
		caption: '编号'
	}, {
		dataField: 'name',
		caption: '用户名'//名称
	}, {
		minWidth: 120,
		caption: '操作',
		buttons: [{
			title: '编辑',
			url: '#/user/edit/{{id}}'//#/路由名称/edit/{{id}}
		}]
	}],
	form: {
		readOnly: false,
		showColonAfterLabel: true,
		showValidationSummary: false,
		colCount: 1,
		items: [{
			dataField: 'name',
			validationRules: [{ type: "required" }],
			label: { text: '用户名' }
		}]
	}
});


App.registerCrud({
	route: 'log',//路由名称
	url: $.config('apiUrl') + 'log',
	title: '日志查询',//分页标题
	capable: {
		create: true
	},
	columns: [{
		dataField: 'id',
		caption: '编号'
	}, {
		dataField: 'date',
		caption: '时间',
		cellTemplate:function(ele,data){
			$(ele).text(moment(data.data.date).format('YYYY-MM-DD HH:mm:ss'))
		}
	},{
		dataField: 'type',
		caption: '功能模块'//名称
	},{
		dataField: 'description',
		caption: '操作记录',
		cellTemplate:function(ele,da){
			let data = JSON.parse(da.data.description);
			let type = data.status?data.status.name||'':'';
			let shift = data.shift?data.shift.name||'':'';
			$(ele).text(`状态:${type},班次:${shift}`)
		}
	},{
		dataField: 'userName',
		caption: '操作人'//名称
	}, {
		minWidth: 120,
		caption: '操作',
		buttons: [{
			title: '编辑',
			url: '#/log/edit/{{id}}'//#/路由名称/edit/{{id}}
		}]
	}],
	form: {
		readOnly: false,
		showColonAfterLabel: true,
		showValidationSummary: false,
		colCount: 1,
		beforeSubmit: function(form, id) {
			form.option('formData', Object.assign({type: 'admin'}, form.option('formData')));

			return true;
		},
		items: [{
			dataField: 'id',
			validationRules: [{ type: "required" }],
			label: { text: '编号' }
		}, {
			dataField: 'date',
			validationRules: [{ type: "required" }],
			editorType: 'dxDateBox',
			label: { text: '时间' }
		},{
			dataField: 'moduleId',
			validationRules: [{ type: "required" }],
			label: { text: '功能模块' }
		},{
			dataField: 'description',
			validationRules: [{ type: "required" }],
			label: { text: '操作记录' }
		},{
			dataField: 'siteName',
			validationRules: [{ type: "required" }],
			label: { text: '站点名称' }
		},{
			dataField: 'employeeId',
			validationRules: [{ type: "required" }],
			label: { text: '操作人' }
		}]
	}
});

App.registerRoute({
	name:'params',
	onLoad:function(context,$container){
		let page = new Page($container);
		page.crudLayout({title:'系统参数'});

		page.content.html(
			'<div class="formSubmit"></div>'+
			'<div class="formBody"></div>'
			);

		let formInstance = 
		page.content.find('.formSubmit').dxForm({
			formData:{},
			colCount:3,
			items:[]
		}).dxForm('instance')

		page.content.find('.formBody').dxToolbar({
			items:[{
				location:'after',
				widget:'dxButton',
				options:{
					type:'success',
					text:'保存',
					onClick:function(){
						let form = formInstance.option('formData')
						Util.postForm($.config('apiUrl')+'system/save',form).then(res=>{
							if(res.errcode ===200){
								new DevExpress.ui.notify({message:'保存成功'},'success',1000)
							}else{
								new DevExpress.ui.notify({message:'保存失败:'+res.errmsg},'warning',1500)
							}
						})
					}
				}
			}]
		})

		$.get($.config('apiUrl')+'system/list').then(res=>{
			let data = res.data;
			formInstance.option({
				formData:data,
				items:[{
					label:{text:'任务间隔时间'},
					dataField:'taskInterval'
				},{
					label:{text:'营业开始时间'},
					dataField:'shopBegin'
				},{
					label:{text:'营业结束时间'},
					dataField:'shopClose'
				},{
					label:{text:'接口Key'},
					dataField:'apiKey'
				},{
					label:{text:'接口URL'},
					dataField:'apiUrl'
				}]
			})
		})
	}
})
